package com.example.demo;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * @author yishuangshuang
 * @ClassName: StreamDemo
 * @Description: TODO
 * @date 2019/1/4 4:21 PM
 */
public class StreamDemo {
    public static void main(String[] args) {
        Apple apple1 = new Apple(1, "苹果1", new BigDecimal("3.25"), 10);
        Apple apple12 = new Apple(1, "苹果2", new BigDecimal("1.35"), 20);
        Apple apple2 = new Apple(2, "香蕉", new BigDecimal("2.89"), 30);
        Apple apple3 = new Apple(3, "荔枝", new BigDecimal("9.99"), 40);

        List<Apple> appleList = new ArrayList<>();
        appleList.add(apple1);
        appleList.add(apple12);
        appleList.add(apple2);
        appleList.add(apple3);

        List<Orange> orangeList = appleList.stream().map(apple -> new Orange(apple.getId(), apple.getName(), apple.getMoney(), apple.getNum())).collect(Collectors.toList());
        System.out.println(orangeList);

        IntStream.rangeClosed(1, 3).forEach(System.out::println);
        IntStream.range(1, 3).forEach(System.out::println);
        IntStream.of(new int[]{1, 2, 3}).forEach(System.out::println);

        // 1. Array
        String[] strArray1 = Stream.of(new String[]{}).toArray(String[]::new);
        // 2. Collection
        List<String> list1 = Stream.of(new String[]{}).collect(Collectors.toList());
        List<String> list2 = Stream.of(new String[]{}).collect(Collectors.toCollection(ArrayList::new));
        Set set1 = Stream.of(new String[]{}).collect(Collectors.toSet());
        Stack stack1 = Stream.of(new String[]{}).collect(Collectors.toCollection(Stack::new));
        // 3. String
        String str = Stream.of(new String[]{}).collect(Collectors.joining()).toString();

        List<String> output = Stream.of(new String[]{"a"}).
                map(String::toUpperCase).
                collect(Collectors.toList());

        // 方法引用是lambda的简化写法。可以是static方法或者public成员方法。
        List<String> d = Stream.of(new String[]{"a"}).
                map(x -> x.toUpperCase()).
                collect(Collectors.toList());

        // flatMap
        Stream<List<Integer>> inputStream = Stream.of(
                Arrays.asList(1),
                Arrays.asList(2, 3),
                Arrays.asList(4, 5, 6)
        );
        Stream<Integer> outputStream = inputStream.
                flatMap((childList) -> childList.stream());

        Integer[] sixNums = {1, 2, 3, 4, 5, 6};
        Integer[] evens =
                Stream.of(sixNums).filter(n -> n % 2 == 0).toArray(Integer[]::new);
        System.out.println(Arrays.toString(evens));

        Stream.of("one", "two", "three", "four")
                .filter(e -> e.length() > 3)
                .peek(e -> System.out.println("Filtered value: " + e))
                .map(String::toUpperCase)
                .peek(e -> System.out.println("Mapped value: " + e))
                .collect(Collectors.toList());

    }
}
